package com.hspedu.tankgame06;

import java.util.Vector;

//自己的坦克
public class Hero extends Tank {
    //创建一个shot对象，表达一个射击行为
    Shot shot = null;

    Vector<Shot> shots = new Vector<>();

    //增加一个成员，使EnemyTank可以获得敌人坦克的Vector
    Vector<EnemyTank> enemyTanks = new Vector<>();

    public Hero(int x, int y) {
        super(x, y);
    }

    public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        this.enemyTanks = enemyTanks;
    }

    //碰撞检测，当我方坦克碰到敌方坦克时爆炸
    public boolean isTouchTank() {
        //获取当前坦克方向
        switch (this.getDirect()) {
            case 0://上
                //当前坦克和其他坦克比较
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                        //敌人坦克上/下
                        //关注x,y的范围
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //1. 当前坦克左上角坐标 [this.getX(),this.getY()]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                //碰撞
                                return true;
                            }
                            //2. 当前坦克右上角坐标 [this.getX() + 40,this.getY()]
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                //碰撞
                                return true;
                            }
                        //敌人坦克左/右
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            //当前坦克左上角坐标
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                //碰撞
                                return true;
                            }
                            //2. 当前坦克右上角坐标 [this.getX() + 40,this.getY()]
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                //碰撞
                                return true;
                            }
                        }
                    }
                }
                break;
            case 1://右
                //当前坦克和其他坦克比较
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                        //敌人坦克上/下
                        //关注x,y的范围
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //1. 当前坦克右上角坐标 [this.getX() + 60,this.getY()]
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                //碰撞
                                return true;
                            }
                            //2. 当前坦克右下角坐标 [this.getX() + 60,this.getY() + 40]
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 40
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 60) {
                                //碰撞
                                return true;
                            }
                        }
                        //敌人坦克左/右
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            ////1. 当前坦克右上角坐标 [this.getX() + 60,this.getY()]
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                //碰撞
                                return true;
                            }
                            //2. 当前坦克右下角坐标 [this.getX() + 60,this.getY() + 40]
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 60
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 40) {
                                //碰撞
                                return true;
                            }
                        }
                    }
                break;
            case 2://下
                //当前坦克和其他坦克比较
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                        //敌人坦克上/下
                        //关注x,y的范围
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //1. 当前坦克左下角坐标 [this.getX(),this.getY() + 60]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 60) {
                                //碰撞
                                return true;
                            }
                            //2. 当前坦克右下角坐标 [this.getX() + 40,this.getY() + 60]
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 40
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 60) {
                                //碰撞
                                return true;
                            }
                        }
                        //敌人坦克左/右
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            ////1. 当前坦克左下角坐标 [this.getX(),this.getY() + 60]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 40) {
                                //碰撞
                                return true;
                            }
                            //2. 当前坦克右下角坐标 [this.getX() + 40,this.getY() + 60]
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 60
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 40) {
                                //碰撞
                                return true;
                            }
                        }
                    }
                break;
            case 3://左
                //当前坦克和其他坦克比较
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                        //敌人坦克上/下
                        //关注x,y的范围
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //1. 当前坦克左上角坐标 [this.getX(),this.getY()]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                //碰撞
                                return true;
                            }
                            //2. 当前坦克右下角坐标 [this.getX(),this.getY() + 40]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 60) {
                                //碰撞
                                return true;
                            }
                        }
                        //敌人坦克左/右
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            ////1. 当前坦克左上角坐标 [this.getX(),this.getY()]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                //碰撞
                                return true;
                            }
                            //2. 当前坦克左下角坐标 [this.getX(),this.getY() + 40]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 40) {
                                //碰撞
                                return true;
                            }
                        }
                    }
                break;
        }
        return false;
    }

    //射击
    public void ShotEnemyTank() {
        //控制面板上子弹最多有五颗
        if (shots.size() == 5) return;

        //根据Hero对象的位置和方向来创建Shot
        switch (getDirect()) {//获取Hero对象的方向
            case 0://上
                shot = new Shot(getX() + 20,getY(),0);
                break;
            case 1://右
                shot = new Shot(getX() + 60,getY() + 20,1);
                break;
            case 2://下
                shot = new Shot(getX() + 20,getY() + 60,2);
                break;
            case 3://左
                shot = new Shot(getX(),getY() + 20,3);
                break;
        }
        //把创建的Shot对象放入到Shots中
        shots.add(shot);
        //启动shot线程
        new Thread(shot).start();
    }
}
